package com.touchwisdom.fetch.file.common;

import com.jfinal.autotable.AutoTableActiveRecordPlugin;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.cron4j.Cron4jPlugin;
import com.jfinal.plugin.hikaricp.HikariCpPlugin;
import com.jfinal.template.Engine;
import com.touchwisdom.fetch.file.model.TFileFetch;
import com.touchwisdom.fetch.file.web.FileFetchController;
import com.touchwisdom.fetch.file.web.IndexController;
import com.touchwisdom.modular.common.CommonConfigKit;
import com.touchwisdom.modular.common.model.SysEntity;
import com.touchwisdom.modular.common.model.SysEntityGrid;

public class FileFetchConfig extends JFinalConfig {
	public static Cron4jPlugin cron4jPlugin;

	@Override
	public void configConstant(Constants me) {
		PropKit.use("application.properties");
		me.setDevMode(PropKit.getBoolean("devMode", true));
	}

	@Override
	public void configRoute(Routes me) {
		me.setBaseViewPath("WEB-INF/content");
		me.add("/", IndexController.class, "/index");
		me.add("/file", FileFetchController.class);
	}

	@Override
	public void configEngine(Engine me) {
		CommonConfigKit.configCommonEngine(me);
	}

	@Override
	public void configPlugin(Plugins me) {
		// Task Plugin
		cron4jPlugin = new Cron4jPlugin();
		me.add(cron4jPlugin);
		// DB plugin
		HikariCpPlugin hikariCp = new HikariCpPlugin(String.format(PropKit.get("jdbcUrl"), PathKit.getWebRootPath()),
				PropKit.get("user"), PropKit.get("password").trim());
		// 加载sqlite的驱动,其他操作和mysql的一样
		hikariCp.setDriverClass("org.sqlite.JDBC");
		me.add(hikariCp);
		AutoTableActiveRecordPlugin arp = new AutoTableActiveRecordPlugin(hikariCp);
		me.add(arp);
		// 基本信息
		arp.addMapping(SysEntity.class);
		arp.addMapping(SysEntityGrid.class);
		// 个性化信息
		arp.addMapping(TFileFetch.class);
	}

	@Override
	public void configInterceptor(Interceptors me) {
	}

	@Override
	public void configHandler(Handlers me) {
		CommonConfigKit.configCommonHandler(me);
	}
}